package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwbpT;
import cn.e4j.modular.crm.mapper.GwbpTMapper;
import cn.e4j.modular.crm.service.GwbpTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  业务人员指定客户设置 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-16
 */
@Service
public class GwbpTServiceImpl extends ServiceImpl<GwbpTMapper, GwbpT> implements GwbpTService {
    @Autowired
    SysAuthorityService sysAuthorityService;
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbpList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwbpDtos = this.baseMapper.listGwbpPage(page, dto);
        return gwbpDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getUserList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> userDtos = this.baseMapper.listUserPage(page, dto);
        return userDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwakList(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));

        List<Map<String, Object>> gwakDtos = this.baseMapper.listGwakPage(page, dto);
        return gwakDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaa(String ID) {
        ShiroUser shiroUser = ShiroKit.getUser();
        List<Map<String, Object>> gwaaDtos = this.baseMapper.getGwaa(shiroUser.getDBC(), ID);
        return gwaaDtos;
    }

    @Transactional(readOnly = true)
    public GwbpT getByKey(GwbpT gwbpT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwbpT> gwbpTUpdateWrapper = new UpdateWrapper<>();
        gwbpTUpdateWrapper.eq("GWBPENT",shiroUser.getDBC());
        gwbpTUpdateWrapper.eq("GWBP001",gwbpT.getGWBP001());
        gwbpTUpdateWrapper.eq("GWBP002",gwbpT.getGWBP002());
        GwbpT gwbp = this.baseMapper.selectOne(gwbpTUpdateWrapper);
        return gwbp;
    }

    @Transactional
    @Override
    public void add(GwbpT gwbpT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbpT.setGWBPENT(shiroUser.getDBC());
        gwbpT.setGWBPUSER(shiroUser.getACCOUNT());
        gwbpT.setGWBPGRUP(shiroUser.getORG_ID());
        gwbpT.setGWBPDATE(LocalDate.now());
        gwbpT.setGWBPTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwbpT.setGWBPORIG(shiroUser.getORG_ID());
        gwbpT.setGWBPORIU(shiroUser.getACCOUNT());

        gwbpT.insert();
    }

    @Transactional
    @Override
    public void edit(GwbpT gwbpT,GwbpT oldGwbpT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbpT.setGWBPMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwbpT> gwbpTUpdateWrapper = new UpdateWrapper<>();
        gwbpTUpdateWrapper.eq("GWBPENT",gwbpT.getGWBPENT());
        gwbpTUpdateWrapper.eq("GWBP001",oldGwbpT.getGWBP001());
        gwbpTUpdateWrapper.eq("GWBP002",oldGwbpT.getGWBP002());

        gwbpT.update(gwbpTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(GwbpT gwbpT) {
        UpdateWrapper<GwbpT> gwbpTUpdateWrapper = new UpdateWrapper<>();
        gwbpTUpdateWrapper.eq("GWBPENT",gwbpT.getGWBPENT());
        gwbpTUpdateWrapper.eq("GWBP001",gwbpT.getGWBP001());
        gwbpTUpdateWrapper.eq("GWBP002",gwbpT.getGWBP002());

        gwbpT.delete(gwbpTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwbpT> gwbpTList) {
        for(GwbpT gwbpT : gwbpTList){
            UpdateWrapper<GwbpT> gwbpTUpdateWrapper = new UpdateWrapper<>();
            gwbpTUpdateWrapper.eq("GWBPENT",gwbpT.getGWBPENT());
            gwbpTUpdateWrapper.eq("GWBP001",gwbpT.getGWBP001());
            gwbpTUpdateWrapper.eq("GWBP002",gwbpT.getGWBP002());

            gwbpT.delete(gwbpTUpdateWrapper);
        }
    }

}
